import {client} from "../../js/client";
Page({
  data: {
    dynasties: [{ text: "先秦" }, { text: "两汉" }, { text: "魏晋" }, { text: "南北朝" }, { text: "隋代" }, { text: "唐代" }, { text: "五代" }, { text: "宋代" }, { text: "金朝" }, { text: "元朝" }, { text: "明代" }, { text: "清代" }, { text: "近代"}],
    types: [{ text: "全部" }, { text: "诗" }, { text: "词" }, { text: "曲" }, { text:  "文言文"}],
    current:5,
    current_type:1,
    gushis: {}//！！！这里是诗词的数据数组，数组元素的属性有id,title, author, dynasty, content, type, like_count, author_id, author_source_id, key, name
  },

  onDynastyChange({detail:{current, value, source}}){
    if(source=="touch"){
      this.options.dynasty = this.data.dynasties[current].text;
      this.setData({current});
      this.refresh({refresh:true});
    }
  },
  onTypeChange({detail:{current, value}}){
    this.options.type = value.text;
    this.setData({current_type:current});
    this.refresh({ refresh: true });
  },
  refresh({refresh=false}={}){
    this.setData({ loading: 1 });
    if(refresh)
      this.options.page = 1;
    else
      this.options.page++;
    client.api("getPoemData")(this.options)
    .then(res=>{
    
      console.log(this.data, res);
      if(refresh)
        var gushis = res.poems.data
      else 
        var gushis = (this.data.gushis[this.data.current]||[]).concat(res.poems.data)
      var key = 'gushis[' + this.data.current + ']';
      this.setData({[key]:gushis, loading:0});
    }).catch(e=>{
      console.error(e);
      this.setData({loading:0});
    })
  },
  onLoad: function (options) {
    this.options = { dynasty: "唐代", type: "诗", page: 1 };
    this.refresh({refresh:true});
  },
  onReady: function () {

  },

  onShow: function () {

  },

  onHide: function () {

  },


  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refresh({refresh:true})
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.refresh();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})